var vm = new Vue({
    //vue对象节点,指向父盒子
    el:'#user',                                                                   
    //vue实例 的data属性
    data:function(){
        return{
          pageA:true,
          pageB:false,
          pageC:false,
          pname:'',
          pdescription:'',
          createDate:[],
	      tableData:[],
	      rowS:{
	    	  height:20
	      },
	      multipleSelection:[],
	      //分页条
	      pageSize:10,
	      currPage:1,
	      total:1,
	      //
	      plate:{},
          statusList:[{value: '0', label: '正常'}],
          plateRules:{ 
	    	  pname: [{ required: true, message: '请输入板块名称', trigger: 'blur' },
			   	   { max: 20, message: '长度不能超过20个字符', trigger: 'blur' }],
			  pdescription: [{ required: true, message: '请输入板块描述', trigger: 'blur' },
		   	     { max: 150, message: '长度不能超过150个字符', trigger: 'blur' }]
	      }
        };
    },
    //vue实例 的methods
    methods:{
       handleSizeChange:function(val) {
          vm.pageSize = val;
          vm.query();
       },
       handleCurrentChange:function(val) {
    	   vm.currPage = val;
    	   vm.query();
       },
       handleSelectionChange(val) {
           vm.multipleSelection = val;
       },
       //格式化日期
       dateFormat:function(row,column){
    	   var date = row[column.property];
    	   if(date == undefined){
    		   return "";
    	   }
    	   return moment(date).format("YYYY-MM-DD HH:mm:ss");
       },
       loadData:function(){
    	   var url = "./listPlate.action";
    	   if(vm.createDate == null || vm.createDate.length ==0 
    			   || typeof(vm.createDate[0]) == 'string' || typeof(vm.createDate[1]) == 'string'){
    		   vm.createDate = ['',''];
		   }else{
			   vm.createDate.splice(0,2,moment(vm.createDate[0]).format('YYYY-MM-DD'),
			   moment(vm.createDate[1]).add(1, 'days').format('YYYY-MM-DD'));
		   };
    	   var params = {
    		   'pagemap.size':vm.pageSize,
    		   'pagemap.page':vm.currPage,
    		   'pagemap.pdescription':vm.pdescription,
    		   'pagemap.pname':vm.pname,
    		   'pagemap.mindate':vm.createDate[0],
    		   'pagemap.maxdate':vm.createDate[1]
    	   }
    	   pb.ajaxPost(url,params,function(r){
	       		if(r.code == '000000'){
	       			/*if(r.plateList !== null &&r.plateList.length>0){
	       				for(var i=0;i<r.plateList.length;i++){
	       					var ai = r.plateList[i];
	       					vm.tableData.push(ai[0]);
	       				}
	       			}*/
	       			vm.tableData = r.plateList;
	       			vm.total = r.platecount;
	       		}
	       		else{
	       			pb.alertErr('加载失败');
	       		}
	       });
       },
       query:function(){
    	   vm.loadData();
       },
       getRecord:function(){
    	   var url = "./getPlateById.action";
    	   var params = {
    		   'plate.pid':vm.multipleSelection[0].pid
    	   }
    	   pb.ajaxPost(url,params,function(r){
	       		if(r.code == '000000'){
	       			vm.plate = r.plate;
	       	
	       		}else{
	       		 pb.alertErr('获取信息失败');
	       		}
	       },function(err){
	    	   pb.alertErr('获取信息失败');
	       });
       },
       //新增
       add:function(){
    	   vm.pageA=false;
    	   vm.pageB=true;
    	   vm.pageC=false;
    	  // vm.plate.pstatus = 0;
       },
       //更新
       update:function(){
    	   if(vm.multipleSelection == null || vm.multipleSelection.length !=1){
    		   pb.alertWar("请选择一条记录");
    		   return false;
    	   }
    	   vm.pageA=false;
    	   vm.pageB=true;
    	   vm.pageC=false;
    	   vm.getRecord();
       },
       //删除
       del:function(){
    	   if(vm.multipleSelection == null || vm.multipleSelection.length <=0){
    		   pb.alertWar("请选择至少一条记录");
    		   return false;
    	   }
    	   var ids = [];
    	   for(var i=0;i<vm.multipleSelection.length;i++){
    		   ids.push(vm.multipleSelection[i].pid);
    	   }
    	   
    	   vm.$confirm('该操作也将删除该板块下的所有发帖，确定删除么?','提示',{
               confirmButtonText: '确定',
               cancelButtonText: '取消',
               type:'warning',
               callback:function(e){
                   if(e === 'confirm'){
                	   var params = {
                		   "res.ids":JSON.stringify(ids)
                			  //"plate.pid":vm.multipleSelection[0].pid
                	   }
                       pb.ajaxPost("./deletePlate.action",params,function(res){
                           if(res.code == '000000'){
                        	   pb.alertSuc('删除成功');
                               vm.query();
                           }else{
                           	pb.alertErr('删除失败');
                           }
                       });
                   	
                   }
               }
           });
       },
       submit:function(formName){
    	   vm.$refs[formName].validate(function(valid) {
 	          if (valid) {
                  var url ="";
 	        	  if(vm.plate.pid == null || vm.plate.pid == ''){
 	        		 url = './addPlate.action';
 	        	  }else{
 	        		 url = './updatePlate.action';
 	        	  }
 	        	  
                  var params = {
                		"plate.pid":vm.plate.pid,
                  		"plate.pname":vm.plate.pname,
                  		"plate.pdescription":vm.plate.pdescription	
                  };
    
                  pb.ajaxPost(url,params,function(r){
                  	if(r.code == '000000'){
                  		pb.alertSuc("提交成功");
                  		vm.back('plateRef');
                  	}else{
                  		pb.alertErr("提交失败");
                  	}
                  	},function(err){

                  		pb.alertErr("操作超时");
                  	});
 	          } else {
 	            return false;
 	          }
 	    });
       },
       back:function(formName){
    	   vm.pageA=true;
    	   vm.pageB=false;
    	   vm.pageC=false;
    	   vm.plate = {};
    	   vm.$refs[formName].resetFields();
    	   vm.loadData(vm.pageSize,vm.currPage);
       }


    }
    //vue实例 的methods结束
});
vm.loadData(10,1);